package edu.mtu.citizenscience.ethnography.util;

import edu.mtu.citizenscience.ethnography.R;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;

public abstract class EthnographyActivity extends FragmentActivity {

	private static final String RESULT_STR = "result string";
	private static final int RESULT_FAIL = Activity.RESULT_FIRST_USER + 1;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_FAIL) {
			if (intent != null) {
				String resultStr = intent.getStringExtra(RESULT_STR);
				if (resultStr != null) {
					Util.showErrorDialog(this, resultStr);
				} else {
					Util.showErrorDialog(this, getString(R.string.unknown_error));
				}
			} else {
				Util.showErrorDialog(this, getString(R.string.unknown_error));
			}
		}
	}

	/**
	 * Sets the activity result to {@link Activity#RESULT_CANCELED}, with a resID for a String
	 * to display to the user in an error dialog, then finishes the activity.
	 */
	protected void fail(int resID) {
		String str = getString(resID);
		Intent intent = new Intent();
		intent.putExtra(RESULT_STR, str);
		setResult(RESULT_FAIL, intent);
		finish();
	}
}
